<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-row layui-col-space5">
                <div class="layui-col-md3">
                    <div class="layui-card-header">科目</div>
                    <table class="layui-hide"  id="left-table" lay-filter="left-table"></table>
                </div>
                <div class="layui-col-md9">
                    <div class="layui-card-header">章节管理</div>
                    <table id="main-table" lay-filter="main-table"></table>
                    <div id="treeTablePage"></div>
                </div>
            </div>
        </div>
    </div>
</div>

<script th:inline="none"></script>
<script>
    layui.extend({
        treeTable: '{/}layuiadmin/plugins/treeTable/treeTable'
    });
    var whr = {};
    var limit = 20;
    layui.use(['table','form','treeTable','laypage'], function(){
        var table = layui.table,
            treeTable = layui.treeTable,
            laypage = layui.laypage,
            form = layui.form;
        table.render({
            elem: '#left-table'
            ,height: (top.height-85)+'px'
            ,url: 'libCategory/getPageList'
            ,title: '标签组列表'
            ,page: true
            ,cols: [
                [
                    {type: 'checkbox'}
                    ,{field: 'name', title: "组名",width:280}
                ]
            ]
            ,done:function(){
                //enableRowClick(table,form,'left-table',"left-table");
            }
        });
        table.on('row(left-table)', function(obj){
            clickLayTableRow(table,form,'left-table',obj);
            loadGrid();
        });
        var insTb =treeTable.render({
            elem: '#main-table',
            tree: {
                iconIndex: 1,
                idName: 'id',       // 自定义id字段的名称
                pidName: 'parentId',
                isPidData: true, // 是否是pid形式数据
                openName: 'orderno'
            },
            toolbar: '#toolbar',
            checkboxCascade : 'none',
            cols: [
                {type: 'numbers'},
                {field: 'orderno',title: "排序",width:120},
                {field: 'name', title: "章节名称",width:280,templet:function(d){
                        return "<a class='colum-link' href='javascript:void(0)' onclick='edit(\""+d.id+"\")'>" + d.name + "</a>";
                    }}
                ,{field: 'orderno',title: "排序",width:120,edit:'number'}
                ,{field: 'createUserName',title: "登记人",width:120}
                ,{field: 'createTime', title: "登记时间",width:170}
                ,{field: 'stateTxt',title: "状态",width:90}
            ],
            height :(top.height-265)+"px",
            done:function(){


            },
            reqData: function (data, callback) {
                axios.get('chapter/getPageList',{params:queryParams(1)}).then(res=> {
                    if (res.data.code == 1) {
                        layer.msg(res.msg);
                    } else {
                        callback(res.data.data);
                        laypage.render({
                            elem: 'treeTablePage'
                            ,count: res.data.count
                            ,limit: limit
                            ,layout: ['prev', 'count','page', 'next', 'limit', 'skip']
                            ,jump: function(obj,first){
                                if(!first){

                                    axios.get('chapter/getPageList',{params:queryParams(obj.curr)}).then(res=> {
                                        if (res.data.code == 1) {
                                            layer.msg(res.msg);
                                        } else {
                                            callback(res.data.data);

                                        }
                                    });
                                }

                            }
                        });
                    }
                })
            },
            style: 'margin-top:0;'
        });
        treeTable.on('edit(main-table)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            axios.post('chapter/save',obj.data).then(
                res=>{
                    layer.msg('保存成功',{icon:1});
                    table.reload('main-table');
                }
            )
        });
        function queryParams(page){
            var checkStatus = table.checkStatus('left-table');
            if(checkStatus.data.length==0){
                whr.categoryId = "-1";
            }else{
                whr.categoryId = checkStatus.data[0].id;
            }
            whr.page = page;
            whr.limit = 20;
            return whr;
        }
        table.on('edit(main-table)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            axios.post('chapter/save',obj.data).then(
                res=>{
                    layer.msg('保存成功',{icon:1});
                    table.reload('main-table');
                }
            )
        });
        treeTable.on('toolbar(main-table)', function(obj){
            var checkStatus = table.checkStatus('left-table');
            let categoryId = checkStatus.data[0].id;
            switch (obj.event){
                case 'add':
                    if(checkStatus.data.length==0){
                        return layer.msg("请先选择题库分类")
                    }
                    top.layer.open({
                        id:"editwin",
                        type: 2,
                        title:'修改章节',
                        area:['800px','600px'],
                        content:  'rooter?url=base/chapter/add-chapter&categoryId='+categoryId,
                        btn:['保存','关闭'],
                        yes:function(index,layero){
                            var dataForm = top.layer.getChildFrame('form', index);
                            dataForm.find("#submitBtn").click();
                            return false
                        }
                    });
                    break;
            }
        })
        window.loadGrid = function(){

            insTb.refresh();
        }



    });
    function edit(id){
        top.layer.open({
            id:"editwin",
            type: 2,
            title:'修改章节',
            area:['800px','600px'],
            content:  'rooter?url=base/chapter/add-chapter&id='+id,
            btn:['保存','关闭'],
            yes:function(index,layero){
                var dataForm = top.layer.getChildFrame('form', index);
                dataForm.find("#submitBtn").click();
                return false
            }
        });
    }

</script>
<script type="text/html" id="toolbar">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-addition"></i>增加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</button>
    </div>
</script>
</body>
</html>